﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

using FRL.DebtChased.Repository;

namespace FRL.DebtChased.Website
{

	/// <summary>
	/// Declaration of MyRoleProvider class
	/// FRL DebtChased-specific implementation of a RoleProvider
	/// </summary>
	public class MyRoleProvider : RoleProvider
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public MyRoleProvider()
		{

		}

		public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
		{

			// Call base-class function
			base.Initialize(name, config);

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override string[] GetRolesForUser(string username)
		{

			// declare return variable
			string[] Ftmp = null;

			try
			{

				// Load user's roles
				Definitions.eRoles Roles = Definitions.eRoles.NotSet;
				using (DebtChasedEntities ent = new DebtChasedEntities())
				{

					// Load user's roles
					Roles = User.GetUserRoles(ent,username);

				}

				// Get names and values
				List<string> names = new List<string>();
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_SYSADMIN))
					names.Add("FIN_SYSADMIN");
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_MNGADMIN))
					names.Add("FIN_MNGADMIN");
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_SUPERUSER))
					names.Add("FIN_SUPERUSER");
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_USERADMIN))
					names.Add("FIN_USERADMIN");
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_USER))
					names.Add("FIN_USER");
				if (Definitions.Contains(Roles, Definitions.eRoles.FIN_SALES))
					names.Add("FIN_SALES");
				if (Definitions.Contains(Roles, Definitions.eRoles.CLIENT_ADMIN))
					names.Add("CLIENT_ADMIN");
				if (Definitions.Contains(Roles, Definitions.eRoles.CLIENT_USER))
					names.Add("CLIENT_USER");
				if (Definitions.Contains(Roles, Definitions.eRoles.DEBTOR))
					names.Add("DEBTOR");
				Ftmp = names.ToArray();

			}
			catch (Exception ex)
			{

				// Coming right back at yu'
				throw ex;

			}

			// Return variable
			return Ftmp;

		}

		public override bool IsUserInRole(string username, string roleName)
		{

			// Return variable
			bool bFtmp = false;

			try
			{

				// Get roles for user
				string[] roles = GetRolesForUser(username);
				for (int i1 = 0; i1 < roles.Length; i1++)
				{
					if (roles[i1] == roleName)
					{
						bFtmp = true;
						break;
					}
				}

			}
			catch
			{

				// Update return variable
				return bFtmp;

			}

			// Return variable
			return bFtmp;

		}

		/////////////////////////////////////////////////////////////
		#region Unimplemented functions

		public override string[] FindUsersInRole(string roleName, string usernameToMatch)
		{
			throw new NotImplementedException();
		}

		public override string[] GetAllRoles()
		{
			throw new NotImplementedException();
		}

		public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
		{
			throw new NotImplementedException();
		}

		public override void AddUsersToRoles(string[] usernames, string[] roleNames)
		{
			throw new NotImplementedException();
		}

		public override string[] GetUsersInRole(string roleName)
		{
			throw new NotImplementedException();
		}

		public override bool RoleExists(string roleName)
		{
			throw new NotImplementedException();
		}

		public override void CreateRole(string roleName)
		{
			throw new NotImplementedException();
		}

		public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
		{
			throw new NotImplementedException();
		}

		public override string ApplicationName
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		#endregion

	}

}